package com.styytech.dadabao.widge.headportrait;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import com.styytech.dadabao.widge.headportrait.ImageGridAdapter.TextCallback;

public class ImageGridActivity extends Activity implements OnClickListener {
	private TextView tv_cancel,tv_done;
	public static final String EXTRA_IMAGE_LIST = "imagelist";

	private List<ImageItem> dataList;
	private GridView gridView;
	private ImageGridAdapter adapter;
	private AlbumHelper helper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_grid);
		initViews();
	}
	private void initViews() {
		tv_cancel=(TextView)findViewById(R.id.tv_cancel);
		tv_done=(TextView)findViewById(R.id.tv_done);
		tv_cancel.setOnClickListener(this);
		tv_done.setOnClickListener(this);
		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());

		dataList = (List<ImageItem>) getIntent().getSerializableExtra(
				EXTRA_IMAGE_LIST);
		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		adapter = new ImageGridAdapter(ImageGridActivity.this, dataList,
				mHandler);
		gridView.setAdapter(adapter);
		adapter.setTextCallback(new TextCallback() {
			public void onListen(int count) {
				if (tv_done != null) {
					tv_done.setText("完成"
							+ "(" + count + ")");
				}
			}
		});

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				adapter.notifyDataSetChanged();
			}

		});

	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_cancel:
			Intent intentFromGallery = new Intent(ImageGridActivity.this,
					TestPicActivity.class);
			startActivity(intentFromGallery);
			finish();
			break;
		case R.id.tv_done:
			ArrayList<String> list = new ArrayList<String>();
			Collection<String> c = adapter.map.values();
			Iterator<String> it = c.iterator();
			for (; it.hasNext();) {
				list.add(it.next());
			}

			if (Bimp.act_bool) {
				Bimp.act_bool = false;
			}
			for (int i = 0; i < list.size(); i++) {
				if (Bimp.drr.size() < DadaApplication.pci_numbers) {
					Bimp.drr.add(list.get(i));
				}
			}
			finish();
			break;
		default:
			break;
		}
	}
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				Toast.makeText(ImageGridActivity.this,
						"最多只能选取"+DadaApplication.pci_numbers+"张",
						400).show();
				break;
			default:
				break;
			}
		}
	};
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			Intent intentFromGallery = new Intent(ImageGridActivity.this,
					TestPicActivity.class);
			startActivity(intentFromGallery);
			finish();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}
}
